$(function () {
    var $userTableForm = $(".user-table-form");
    var settings = {
        url: ctx + "user/list",
        pageSize: 20,
        queryParams: function (params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                username: $userTableForm.find("input[name='username']").val().trim(),
            };
        },
        onLoadSuccess: function(){
            $('[data-toggle="tooltip"]').tooltip();
        },
        columns: [{
            checkbox: true
        }, {
            field: 'userId',
            visible: false
        }, {
            field: 'username',
            title: '账号'
        }, {
            field: 'nickname',
            title: '用户名'
        }, {
            field: 'deptName',
            title: '部门'
        }, {
            field: 'mobile',
            title: '手机号'
        },{
            field: 'grade',
            title: '账号等级'
        }, {
            field: 'byCode',
            title: '邀请码'
        }, {
            field: 'status',
            title: '状态',
            formatter:function (val,row,idx) {
                var userid = row.userId;
                return val =="1"?"<a href='#' onclick='verifyPass(this)' data='0' spcid='"+userid+"'><i class='fa fa-13x fa-check-square-o text-success' data-toggle='tooltip' title='点击锁定用户'></i></a>"
                    :"<a href='#' onclick='verifyPass(this)' data='1'  spcid='"+userid+"'><i class='fa fa-13x fa-ban text-danger' data-toggle='tooltip' title='点击激活用户'></i></a>";
            }
        }, {
            field: 'crateTime',
            title: '创建时间'
        }]
    };
    $JuXue.initTable('user-data-table', settings);
});
/**刷新**/
function userRefresh() {$(".user-table-form")[0].reset();$JuXue.refreshTable('user-data-table');}
/**删除**/
function deleteUsers() {
    var selected = $("#user-data-table").bootstrapTable('getSelections');
    var selected_length = selected.length;
    var contain = false;
    if (!selected_length) {$JuXue.n_danger('请勾选需要删除的用户！');return;}
     ids = "";
    for (var i = 0; i < selected_length; i++) {
        ids += selected[i].userId;
        if (i !== (selected_length - 1)) ids += ",";
        if (userName === selected[i].username) contain = true;
    }
    if (contain) {$JuXue.n_danger('勾选用户中包含当前登录用户，无法删除！');return;}
    $JuXue.confirm({
        text: "确定删除选中用户？",
        confirmButtonText: "确定删除"
    }, function () {
        $.post(ctx + 'user/delete', {"ids": ids}, function (r) {
            if (r.code === 0) {$JuXue.n_success(r.msg);userRefresh();} else {$JuXue.n_danger(r.msg);}
        });
    });
}
/**导出excel**/
function exportUserExcel() {
    $.post(ctx + "user/excel", $(".user-table-form").serialize(), function (r) {
        if (r.code === 0) {window.location.href = "file/download?fileName=" + r.msg + "&delete=" + true;} else {$JuXue.n_danger(r.msg);}
    });
}
/**新增用户***/
function addUsers() {
    $.post(ctx+"user/addDialog",{},function(r){$("#user_info").empty().append(r).modal("show").draggable();});
}
/**编辑用户**/
function editUsers() {
    var selected = $("#user-data-table").bootstrapTable('getSelections');
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的用户！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一个用户！');return;}
    $.post(ctx+"user/updateDialog",{},function (r) {$("#user_info").empty().append(r).modal("show").draggable();});
}
/**激活用户**/
function verifyPass(btn){
    var userid = $(btn).attr("spcid");
    var data = $(btn).attr("data");
    $JuXue.confirm({
        text: "确定要修改选中的数据？",
        confirmButtonText: "确定修改"
    }, function() {
        $.post(ctx + 'user/updateUserProfile', { "userId": userid,"status":data }, function(r) {
            if (r.code === 0) {$JuXue.n_success(r.msg);$JuXue.refreshTable("user-data-table");} else {$JuXue.n_danger(r.msg);}
        });
    });
}
/**修改密码**/
function modifyUserPassword(){
    var selected = $("#user-data-table").bootstrapTable('getSelections');
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的用户！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一个用户！');return;}
    sessionStorage.setItem("usernameUpdate",selected[0].username);
    $.post(ctx + "user/moPassword", function (r) {$("#index_user_password").empty().append(r).modal("show")});
}